/**
 * 2019-04-23 22:42:48
 */
package com.cdxy.config.swagger;

import com.cdxy.model.dto.base.exception.enums.CommonErrorEnum;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Esion
 */
@Configuration
@EnableSwagger2
public class SwaggerCofing {

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    /**
     * Api 接口文档信息
     *
     * @return .
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("毕业设计 接口文档")
                .description("接口文档")
                .version("2.0")
                .build();
    }

    /**
     * yyk 统一响应
     *
     * @return .
     */
    private List<ResponseMessage> yykResponseMessage () {
        List<ResponseMessage> list = new ArrayList<>();
        for (CommonErrorEnum specialError : CommonErrorEnum.values()) {
            list.add(new ResponseMessageBuilder()
                    .code(Integer.valueOf(specialError.getCode()))
                    .message(specialError.getDesc())
                    .build());
        }
        return list;
    }

    public Set<String> getConsumes() {
        Set<String> consumer = new HashSet<>();
        consumer.add("application/x-www-form-urlencoded");
        return consumer;
    }

    public Set<String> getProduces() {
        Set<String> consumer = new HashSet<>();
        consumer.add("application/json");
        return consumer;
    }

    /**
     * 认证信息
     *
     * @return .
     */
    @Bean
    public List<ApiKey> securitySchemes() {
        return Lists.newArrayList(
                new ApiKey("用户验证信息", "Authorization", "header"),
                new ApiKey("设备验证信息", "Security", "header")
        );
    }


    /**
     * 认证配置上下文
     *
     * @return .
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> list = new ArrayList<>();
        list.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))
                .build());
        return list;
    }


    /**
     * 默认认证信息配置
     *
     * @return .
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
    }


    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .useDefaultResponseMessages(false)
                .groupName("App 接口")
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.cdxy.controller")) // 这里写的是API接口所在的包位置
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

}
